package com.wust.user;

import java.util.ArrayList;
import java.util.List;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;



public class UserDao{
	private Context context;
	private List<User> diarylist;
	public UserDao(Context context)
	{
		this.context=context;
	}
	public void insertUser(User user)
	{
		System.out.println("insert");
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		ContentValues value=new ContentValues();
		value.put("user_name", user.getUser_name());
		value.put("user_pass", user.getUser_pass());
		value.put("user_sex", user.getUser_sex());
		value.put("user_email", user.getUser_email());
		value.put("user_nickname", user.getUser_nickname());
		value.put("user_icon", user.getUser_icon());
		value.put("user_haslogin", user.getUser_haslogin());
		db.insert("da_user", null, value);
		db.close();
	}
	public void updateUser(User user)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		ContentValues value=new ContentValues();
		value.put("user_name", user.getUser_name());
		value.put("user_pass", user.getUser_pass());
		value.put("user_sex", user.getUser_sex());
		value.put("user_email", user.getUser_email());
		value.put("user_nickname", user.getUser_nickname());
		value.put("user_icon", user.getUser_icon());
		value.put("user_haslogin", user.getUser_haslogin());
		db.update("da_user", value, "user_id=?", new String[]{String.valueOf(user.getUser_id())});
		db.close();
	}
	public List<User> queryUser()
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query("da_user", new String[]{"user_id","user_name","user_pass","user_sex","user_email",
				"user_nickname","user_icon","user_haslogin"}, 
				null, null, null, null, "user_id asc");
		List<User> userlist=new ArrayList<User>();
		while(cursor.moveToNext())
		{
			User user=new User();
			user.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
			user.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			user.setUser_pass(cursor.getString(cursor.getColumnIndex("user_pass")));
			user.setUser_sex(cursor.getString(cursor.getColumnIndex("user_sex")));
			user.setUser_email(cursor.getString(cursor.getColumnIndex("user_email")));
			user.setUser_nickname(cursor.getString(cursor.getColumnIndex("user_nickname")));
			user.setUser_icon(cursor.getString(cursor.getColumnIndex("user_icon")));
			user.setUser_haslogin(cursor.getInt(cursor.getColumnIndex("user_haslogin")));
			userlist.add(user);
		}
		db.close();
		return userlist;
	}
	public User getUserById(String user_id)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query("da_user", new String[]{"user_id","user_name","user_pass","user_sex","user_email",
				"user_nickname","user_icon","user_haslogin"}, 
				"user_id=?", new String[]{user_id}, null, null, "user_id asc");
		User user=new User();
		while(cursor.moveToNext())
		{
			
			user.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
			user.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			user.setUser_pass(cursor.getString(cursor.getColumnIndex("user_pass")));
			user.setUser_sex(cursor.getString(cursor.getColumnIndex("user_sex")));
			user.setUser_email(cursor.getString(cursor.getColumnIndex("user_email")));
			user.setUser_nickname(cursor.getString(cursor.getColumnIndex("user_nickname")));
			user.setUser_icon(cursor.getString(cursor.getColumnIndex("user_icon")));
			user.setUser_haslogin(cursor.getInt(cursor.getColumnIndex("user_haslogin")));
			
		}
		db.close();
		return user;
	}
	public User getUserByName(String user_name)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query("da_user", new String[]{"user_id","user_name","user_pass","user_sex","user_email",
				"user_nickname","user_icon","user_haslogin"}, 
				"user_name=?", new String[]{user_name}, null, null, "user_id asc");
		User user=new User();
		while(cursor.moveToNext())
		{
			
			user.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
			user.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			user.setUser_pass(cursor.getString(cursor.getColumnIndex("user_pass")));
			user.setUser_sex(cursor.getString(cursor.getColumnIndex("user_sex")));
			user.setUser_email(cursor.getString(cursor.getColumnIndex("user_email")));
			user.setUser_nickname(cursor.getString(cursor.getColumnIndex("user_nickname")));
			user.setUser_icon(cursor.getString(cursor.getColumnIndex("user_icon")));
			user.setUser_haslogin(cursor.getInt(cursor.getColumnIndex("user_haslogin")));
			
		}
		db.close();
		return user;
	}
	public User checkUserHasLogin()
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query("da_user", new String[]{"user_id","user_name","user_pass","user_sex","user_email",
				"user_nickname","user_icon","user_haslogin"}, 
				"user_haslogin=?", new String[]{"1"}, null, null, "user_id asc");
		User user=new User();
		while(cursor.moveToNext())
		{
			
			user.setUser_id(cursor.getInt(cursor.getColumnIndex("user_id")));
			user.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			user.setUser_pass(cursor.getString(cursor.getColumnIndex("user_pass")));
			user.setUser_sex(cursor.getString(cursor.getColumnIndex("user_sex")));
			user.setUser_email(cursor.getString(cursor.getColumnIndex("user_email")));
			user.setUser_nickname(cursor.getString(cursor.getColumnIndex("user_nickname")));
			user.setUser_icon(cursor.getString(cursor.getColumnIndex("user_icon")));
			user.setUser_haslogin(cursor.getInt(cursor.getColumnIndex("user_haslogin")));
			
		}
		db.close();
		return user;
	}
}